import { Toast } from "vant";
import { ref } from "vue";
import router from "@/router";
import store from "@/store";
import { updateLineStatus } from "@/request/header-request";

const user = store.state.user;
// 在线状态

export const useToggleSheetItem = (s, D, _id) => {
  s.value =
  store.state.user.userInfo.line_status === "online"
      ? "var(--line-color)"
      : "var(--outline-color)";
  D.line_text = store.state.user.userInfo.line_status === "online" ? "在线" : "离线";
  return async (item) => {
    try {
      D.sheet_show = false;
      if (item.index === 1) {
        router.push("/create_group");
      }
      if (item.index === 2) {
        router.push("/add_friend");
      }
      if (item.index === 3) {
        router.push("/draw");
      }
      if (item.index === 4) {
        let status = store.state.user.userInfo.line_status;
        let stayStatus = status === "online" ? "outline" : "online";
        const result = await updateLineStatus(
          _id,
          stayStatus,
          user.userInfo.qq_account
        );
        if (result.code === 201 && result.message === "success") {
          result.data.avatar =
            "http://q4.qlogo.cn/headimg_dl?dst_uin=" +
            result.data.qq_account +
            "&spec=100";
          store.dispatch("user/setUserInfo", result.data);
          localStorage.setItem("userInfo", JSON.stringify(result.data));
          s.value =
          store.state.user.userInfo.line_status === "online"
              ? "var(--line-color)"
              : "var(--outline-color)";
          D.line_text = stayStatus === "online" ? "在线" : "离线";
          Toast(D.line_text);
        } else {
          Toast("离线失败,请尝试刷新重试");
        }
      }
    } catch (error) {
      Toast(error.message);
    }
  };
};
